<template>
  <el-dialog
    :title="!dataForm.logisticsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="物流信息" prop="logisticsMessage">
      <el-input v-model="dataForm.logisticsMessage" placeholder="物流信息"></el-input>
    </el-form-item>
    <el-form-item label="商品订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="商品订单id"></el-input>
    </el-form-item>
    <el-form-item label="发货时间时间" prop="logisticsTime">
      <el-input v-model="dataForm.logisticsTime" placeholder="发货时间时间"></el-input>
    </el-form-item>
    <el-form-item label="物流费用" prop="logisticsPrice">
      <el-input v-model="dataForm.logisticsPrice" placeholder="物流费用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          logisticsId: 0,
          logisticsMessage: '',
          orderId: '',
          logisticsTime: '',
          logisticsPrice: ''
        },
        dataRule: {
          logisticsMessage: [
            { required: true, message: '物流信息不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '商品订单id不能为空', trigger: 'blur' }
          ],
          logisticsTime: [
            { required: true, message: '发货时间时间不能为空', trigger: 'blur' }
          ],
          logisticsPrice: [
            { required: true, message: '物流费用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.logisticsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.logisticsId) {
            this.$http({
              url: this.$http.adornUrl(`/goods/logistics/info/${this.dataForm.logisticsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.logisticsMessage = data.logistics.logisticsMessage
                this.dataForm.orderId = data.logistics.orderId
                this.dataForm.logisticsTime = data.logistics.logisticsTime
                this.dataForm.logisticsPrice = data.logistics.logisticsPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/logistics/${!this.dataForm.logisticsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'logisticsId': this.dataForm.logisticsId || undefined,
                'logisticsMessage': this.dataForm.logisticsMessage,
                'orderId': this.dataForm.orderId,
                'logisticsTime': this.dataForm.logisticsTime,
                'logisticsPrice': this.dataForm.logisticsPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
